package com.qf.service.impl;

import com.qf.service.BusinessService;
import com.qf.service.MemberService;
import com.qf.utils.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private MemberService memberService;

    @Override
    public Map<String, Object> getData() throws InterruptedException {
        //获取运行报表数据
        Map<String,Object> data = new HashMap<>();

        //先创建一个门闩对象
        CountDownLatch countDownLatch = new CountDownLatch(11);

        //获取预约相关数据
        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long todayAddMemberCount = memberService.todayAddMemberCount(); //2s
                data.put("todayNewMember",todayAddMemberCount);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long weekAddMemberCount = memberService.weekAddMemberCount(); //3s
                data.put("thisWeekNewMember",weekAddMemberCount);
                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long monthAddMemberCount = memberService.monthAddMemberCount(); //1s
                data.put("thisMonthNewMember",monthAddMemberCount);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long memberCount = memberService.memberCount(); //2s
                data.put("totalMember",memberCount);

                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long todayOrderNumber = memberService.todayOrderNumber(); //2s
                data.put("todayOrderNumber",todayOrderNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long todayVisitsNumber = memberService.todayVisitsNumber(); //2s
                data.put("todayVisitsNumber",todayVisitsNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long thisWeekOrderNumber = memberService.thisWeekOrderNumber(); //2s
                data.put("thisWeekOrderNumber",thisWeekOrderNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long thisWeekVisitsNumber = memberService.thisWeekVisitsNumber(); //2s
                data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long thisMonthOrderNumber = memberService.thisMonthOrderNumber(); //2s
                data.put("thisMonthOrderNumber",thisMonthOrderNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long thisMonthVisitsNumber = memberService.thisMonthVisitsNumber(); //2s
                data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        //获取热门套餐数据
        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                long thisMonthVisitsNumber = memberService.thisMonthVisitsNumber(); //2s
                data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        ThreadPoolUtils.getThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                List<Map> hotSetmeal = memberService.findHotSetmeal(); //2s
                data.put("hotSetmeal",hotSetmeal);

                //将门闩数量-1
                countDownLatch.countDown();
            }
        });

        //串行 8s ---> 3.+s
        //n个线程，每个线程内部随机休眠Mb秒，计算n个线程最小执行时长
        //一个线程等待其他n个线程执行完后，再继续向下执行 -- CountDownLatch



        countDownLatch.await(); //利用门闩对象在这里等待，等待所有任务都执行完后，再继续向下执行

        return data;
    }
}
